/*
 * Created on 2005-6-6
 * Last modified on 2009-11-5
 * Powered by YeQiangWei.com
 */
package com.yeqiangwei.commons.util;

import java.util.HashMap;

import org.apache.log4j.Logger;

public class TypeChange {

	private static Logger logger = Logger.getLogger(TypeChange.class.getName());

	public static final long LONG_ZERO = new Long(0);
	public static final int INT_ZERO = 0;
	public static final double DOUBLE_ZERO = 0;
	public static final short SHORT_ZERO = 0;
	public static final byte BYTE_ZERO = 0;
	
	public static void main(String args[]){
		//System.out.println(TypeChange.stringToInteger("09", 0));
	}
	
	@SuppressWarnings("unchecked")
	@Deprecated
	public static HashMap<Object, HashMap> objToHashMap(Object o) {
		try {
			return (HashMap) o;
		} catch (Exception e) {
			logger.error(e.toString());
		}
		return null;
	}

	@Deprecated
	public static int objToInt(Object o) {
		if (o != null) {
			try {
				return TypeChange.stringToInt(String.valueOf(o));
			} catch (Exception e2) {
				logger.error(e2.toString());
			}
		}
		return TypeChange.INT_ZERO;
	}

	@Deprecated
	public static double objToDouble(Object o) {
		if (!ValidatorHelper.isEmpty(o)) {
			try {
				return ((Double) o).doubleValue();
			} catch (Exception e) {
				logger.error(e.toString());
			}
		}
		return TypeChange.DOUBLE_ZERO;
	}

	@Deprecated
	public static short objToShort(Object o) {
		if (o != null) {
			try {
				return ((Short) o).shortValue();
			} catch (Exception e) {
				logger.error(e.toString());
			}
		}
		return TypeChange.SHORT_ZERO;
	}

	@Deprecated
	public static String objOfString(Object o) {
		if (o != null) {
			return String.valueOf(o);
		} else {
			return StringHelper.EMPTY;
		}
	}

	@Deprecated
	public static boolean objToBoolean(Object o) {
		if (o != null) {
			try {
				return ((Boolean) o).booleanValue();
			} catch (Exception e) {
				logger.error(e.toString());
			}
		}
		return false;
	}

	public static String ifNullToEmpty(String str) {
		if (str == null) {
			return StringHelper.EMPTY;
		} else {
			return str;
		}
	}

	public static Byte stringToByte(String str, Byte def) {
		if (str != null) {
			try {
				return Byte.parseByte(str);
			} catch (Exception e) {
				logger.error(e.toString());
			}
		}
		return def;
	}

	public static byte stringToByte(String str) {
		if (str != null) {
			try {
				return Byte.parseByte(str);
			} catch (Exception e) {
				logger.error(e.toString());
			}
		}
		return TypeChange.BYTE_ZERO;
	}

	/**
	 * Object to Boolean, default bea;
	 * 
	 * @param object
	 * @param bea
	 * @return
	 */
	public static Boolean objectToBoolean(Object object, Boolean bea) {
		if (object != null && object.toString().length() > 0 
				&& !object.toString().equalsIgnoreCase("null")) 
		{
			try {
				String tmp = String.valueOf(object);
				if (tmp.equals("0")) {
					return false;
				} else if (tmp.equals("1")) {
					return true;
				} else {
					return new Boolean(tmp);
				}
			} catch (RuntimeException e) {
				logger.error(e.toString());
			}
		}
		return bea;
	}

	public static Boolean objectToBoolean(Object object) {
		return stringToBoolean(String.valueOf(object));
	}

	public static Boolean stringToBoolean(String str) {
		return stringToBoolean(str, false);
	}

	public static Boolean stringToBoolean(String str, Boolean def) {
		if (str != null) {
			if (str.equals("1")) {
				return true;
			} else if (str.equals("0")) {
				return false;
			} else if (str.equals("true")) {
				return true;
			} else if (str.equals("false")) {
				return false;
			} else {
				try {
					return Boolean.parseBoolean(str);
				} catch (Exception e) {
					logger.error(e.toString());
				}
			}
		}
		return def;
	}

	/**
	 * 
	 * @param str
	 * @param radix
	 *            进制
	 * @param defaultValue
	 * @return
	 */
	public static Long stringToLong(String str, int radix, Long defaultValue) {
		if (!ValidatorHelper.isEmpty(str)) {
			if (radix > 10) {
				return Long.parseLong(str.trim(), radix);
			} else if (Utils.isNumber(str)) {
				return Long.parseLong(str.trim());
			}
		}
		return defaultValue;
	}

	public static Long stringToLong(String str) {
		return stringToLong(str, 10, TypeChange.LONG_ZERO);
	}

	public static Long stringToLong(String str, Long defaultValue) {
		return stringToLong(str, 10, defaultValue);
	}
	
	public static Long stringToLong(String str, long defaultValue) {
		return stringToLong(str, 10, defaultValue);
	}

	public static Integer objectToInteger(Object obj, Integer defaultValue) {
		if (!ValidatorHelper.isEmpty(obj) && Utils.isNumber(String.valueOf(obj))) {
			try {
				return new Integer(String.valueOf(obj));
			} catch (RuntimeException e) {
				logger.error(e.toString());
			}
		}
		return defaultValue;
	}

	public static int objectToInt(Object obj, int defaultValue) {
		if (!ValidatorHelper.isEmpty(obj) && Utils.isNumber(String.valueOf(obj))) {
			return new Integer(String.valueOf(obj));
		}
		return defaultValue;
	}

	public static short objectToShort(Object obj, int defaultValue) {
		if (!ValidatorHelper.isEmpty(obj) && Utils.isNumber(String.valueOf(obj))) {
			try {
				return new Short(String.valueOf(obj));
			} catch (RuntimeException e) {
				logger.error(e.toString());
			}
		}
		return TypeChange.SHORT_ZERO;
	}
	
	public static long objectToLong(Object obj, Integer defaultValue) {
		if (obj != null) {
			try {
				return new Long(String.valueOf(obj));
			} catch (RuntimeException e) {
				logger.error(e.toString());
			}
		}
		return defaultValue;
	}

	public static long objectToLong(Object obj, Long defaultValue) {
		if (obj != null) {
			try {
				return new Long(String.valueOf(obj));
			} catch (RuntimeException e) {
				logger.error(e.toString());
			}
		}
		return defaultValue;
	}

	public static int stringToInt(String str) {
		if (!ValidatorHelper.isEmpty(str)) {
			return stringToInt(str.trim(), TypeChange.INT_ZERO);
		}
		return TypeChange.INT_ZERO;
	}

	public static Integer stringToInteger(String str, Integer itg) {
		if (!ValidatorHelper.isEmpty(str) && Utils.isNumber(str)) {
			try {
				return Integer.parseInt(str);
			} catch (Exception e) {
				logger.error(e.toString());
			}
		}
		return itg;
	}

	public static int stringToInt(String str, int i2) {
		if (!ValidatorHelper.isEmpty(str) && Utils.isNumber(str)) {
			try {
				return Integer.parseInt(str);
			} catch (Exception e) {
				logger.error(e.toString());
			}
		}
		return i2;
	}

	public static short stringToShort(String str) {
		if (str != null && Utils.isNumber(str)) {
			try {
				return Short.parseShort(str.trim());
			} catch (Exception e) {
				logger.error(e.toString());
			}
		}
		return TypeChange.SHORT_ZERO;
	}

	public static Short stringToShort(String str, Short def) {
		if (str != null && Utils.isNumber(str)) {
			try {
				return Short.parseShort(str.trim());
			} catch (Exception e) {
				logger.error(e.toString());
			}
		}
		return def;
	}

	public static Double stringToDouble(String str, Double def) {
		if (!Utils.isNull(str)) {
			try {
				return Double.parseDouble(str.trim());
			} catch (Exception e) {
				logger.error(e.toString());
			}
		}
		return def;
	}

	public static double stringToDouble(String str) {
		if (!Utils.isNull(str)) {
			try {
				return Double.parseDouble(str.trim());
			} catch (Exception e) {
				logger.error(e.toString());
			}
		}
		return TypeChange.DOUBLE_ZERO;
	}

	public static String intToString(int i) {
		return String.valueOf(i);
	}

	public static long doubleToLong(Double d) {
		try {
			// double转换成long前要过滤掉double类型小数点后数据
			//return Long.parseLong(String.valueOf(d).substring(0, String.valueOf(d).lastIndexOf(".")));
			return d.longValue();
		} catch (Exception e) {
			logger.error(e.toString());
		}
		return TypeChange.LONG_ZERO;
	}

	public static int doubleToInt(Double d) {
		try {
			// double转换成long前要过滤掉double类型小数点后数据
			//return Integer.parseInt(String.valueOf(d).substring(0, String.valueOf(d).lastIndexOf(".")));
			return d.intValue();
		} catch (Exception e) {
			logger.error(e.toString());
		}
		return TypeChange.INT_ZERO;
	}

	public static double longToDouble(Long d) {
		try {
			//return Double.parseDouble(String.valueOf(d));
			return d.doubleValue();
		} catch (Exception e) {
			logger.error(e.toString());
		}
		return TypeChange.DOUBLE_ZERO;
	}

	public static int longToInt(Long d) {
		try {
			//return Integer.parseInt(String.valueOf(d));
			return d.intValue();
		} catch (Exception e) {
			logger.error(e.toString());
		}
		return TypeChange.INT_ZERO;
	}

	public static String longToString(long li) {
		try {
			return String.valueOf(li);
		} catch (Exception e) {
			logger.error(e.toString());
		}
		return StringHelper.EMPTY;
	}

	/**
	 * 
	 * @param str
	 * @param i
	 * @return
	 */
	public static Integer StringToInteger(String str, int i) {
		if(str == null) {
			return i;
		}
		else {
			return stringToInt(str, 0);
		}
	}

}
